const { app, protocol, BrowserWindow, globalShortcut } = require('electron');
const path = require('path')

// import installExtension, { VUEJS3_DEVTOOLS } from 'electron-devtools-installer';
// const installExtension = require('electron-devtools-installer').default;
// const { VUEJS3_DEVTOOLS } = require('electron-devtools-installer');

const isDevelopment = process.env.NODE_ENV !== 'production';
let mainWindow;

// 注册应用的协议
console.log(path.join(__dirname, '../dist', 'index.html'), 122222221)
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
]);

// 禁用代理
app.commandLine.appendSwitch('no-proxy-server');

// 创建主窗口的函数
async function createWindow() {
  mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      allowRunningInsecureContent: true, // 允许加载不安全内容
      sandbox: false,        // 禁用沙箱模式
    }
  });
  // mainWindow.webContents.openDevTools()

  // await mainWindow.loadURL(path.join(__dirname, '../dist', 'index.html'));
  await mainWindow.loadURL('http://localhost:8082');

  if (isDevelopment) {
    globalShortcut.register('F5', () => {
      if (mainWindow) mainWindow.webContents.reload();
    });
  }

  mainWindow.webContents.openDevTools()

  mainWindow.maximize();//默认最大化
}

// Electron 启动后执行的逻辑
app.on('ready', async () => {
  if (isDevelopment && !process.env.IS_TEST) {
    try {
      // await installExtension(VUEJS3_DEVTOOLS);
    } catch (e) {
      console.error('Vue Devtools failed to install:', e.toString());
    }
  }
  try {
    createWindow();
  } catch (error) {
    console.log(error)
  }
});

// 处理关闭事件
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

// 激活应用时重新打开窗口
app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
